package com.cat.model.web.action.admin;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.cat.model.biz.logic.BasLogic;
import com.cat.model.biz.service.admin.IndexRecomService;
import com.cat.model.common.model.common.BasConfigVO;
import com.cat.model.common.model.common.IndexRecomVO;
import com.cat.model.common.util.Constant;
import com.cat.model.common.util.ImageUtil;
import com.cat.model.common.util.StaticFileUtil;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;

public class IndexRecomManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;

	private IndexRecomVO indexRecom;
	private Long id;
	private File image;
	private String imageFileName; 
	private String imageUrl;
	private String cityCode;

		
	@Autowired
	private IndexRecomService indexRecomService; 
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	@Autowired
	private BasLogic basLogic; 
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		if(StringUtils.isNotEmpty(cityCode)){
			condition.put("cityCode", cityCode);
		}
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchIndexRecomSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<IndexRecomVO> list = (List<IndexRecomVO>)plmCommDAO.search("manager.searchIndexRecom", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		
		List<BasConfigVO> cityCodeList =   basLogic.getBasConfigByName(Constant.CONFIG_NAME_CITY_CODE);
		request.setAttribute("cityCodeList", cityCodeList);
		return "list";
	}
	
	public String deleteSingle() {
		indexRecomService.deleteIndexRecom(id);
		okAlert("删除成功！");
		return "list";
	}
	
	public String toAdd(){
		List<BasConfigVO> cityCodeList =   basLogic.getBasConfigByName(Constant.CONFIG_NAME_CITY_CODE);
		request.setAttribute("cityCodeList", cityCodeList);
		request.setAttribute("webUrl", StaticFileUtil.staticRootUrl());
		return "add";
	}
	
	public String add(){
		try {
			if(image != null){
				BufferedImage img = ImageIO.read(new FileInputStream(image));
				if(img == null){
					this.printHTML("<script>parent.callback(" + false + ", '"+"图片格式不正确"+"')</script>");
					return "list";
				}
			} else {
				indexRecom.setImage(modelMapper.getModelInfo(indexRecom.getModelId()).getCoverImage());
			}
			indexRecomService.createIndexRecom(indexRecom, image, imageFileName);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "保存成功");
		return "list";
	}
	
	public String toEdit(){
		indexRecom = modelMapper.getIndexRecom(id);
		if(indexRecom != null){
			imageUrl = ImageUtil.wrapImageUrl(indexRecom.getImage());
			request.setAttribute("modelName", modelMapper.getModelInfo(indexRecom.getModelId()).getName());
		}
		List<BasConfigVO> cityCodeList =   basLogic.getBasConfigByName(Constant.CONFIG_NAME_CITY_CODE);
		request.setAttribute("cityCodeList", cityCodeList);
		request.setAttribute("webUrl", StaticFileUtil.staticRootUrl());
		return "edit";
	}
	
	public String edit(){
		try {
			if(image != null){
				BufferedImage img = ImageIO.read(new FileInputStream(image));
				if(img == null){
					this.printHTML("<script>parent.callback(" + false + ", '"+"图片格式不正确"+"')</script>");
					return "list";
				}
			} else {
				indexRecom.setImage(modelMapper.getModelInfo(indexRecom.getModelId()).getCoverImage());
			}
			indexRecomService.updateIndexRecom(indexRecom, image, imageFileName);
		} catch (Exception e) {
			e.printStackTrace();
			parentAlert(false, e.getMessage());
			return "list";
		}
		parentAlert(true, "保存成功");
		return "list";
	}


	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public IndexRecomVO getIndexRecom() {
		return indexRecom;
	}

	public void setIndexRecom(IndexRecomVO indexRecom) {
		this.indexRecom = indexRecom;
	}

	public File getImage() {
		return image;
	}

	public void setImage(File image) {
		this.image = image;
	}

	public String getImageFileName() {
		return imageFileName;
	}

	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	public String getImageUrl() {
		return imageUrl;
	}

	public void setImageUrl(String imageUrl) {
		this.imageUrl = imageUrl;
	}

	public String getCityCode() {
		return cityCode;
	}

	public void setCityCode(String cityCode) {
		this.cityCode = cityCode;
	}
}
